/*
 * Project Name: cms_web
 * File Name: PictureAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月19日 下午3:08:10
 */
package com.shishuo.cms.action.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.PictureEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.PictureService;
@Controller
@RequestMapping("/admin/picture")
public class PictureAction extends AdminBaseAction
{
	@InitBinder("pictureEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("pictureEntity.");
	}

	@Autowired
	private PictureService pictureService;

	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addPictureEntity(ModelMap modelMap) throws Exception {
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("picture");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/picture/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewPicture(PictureEntity pictureEntity) throws Exception{
		JsonVo<String> json = new JsonVo<String>();
		if (pictureEntity.getPicUrl().equals("")) {
			json.getErrors().put("picUrl", "图片地址不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		pictureService.insert(pictureEntity);
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", pictureService.getAllListPage(pageNum));
		return "system/picture/all";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> delete(
			@RequestParam(value = "id") long id) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		pictureService.deleteById(id);
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(@RequestParam(value = "id") long id,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		PictureEntity pictureEntity=pictureService.getById(id);
		modelMap.put("pictureEntity", pictureEntity);
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("picture");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/picture/update";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> update(
			@ModelAttribute PictureEntity pictureEntity,
			@RequestParam(value = "content", defaultValue = "") String content) throws Exception {
		pictureEntity.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (pictureEntity.getName() == null
				|| "".equals(pictureEntity.getName())) {
			json.getErrors().put("name", "名称不能为空");
		}
		if (pictureEntity.getPicUrl() == null
				|| "".equals(pictureEntity.getPicUrl())) {
			json.getErrors().put("picUrl", "图片不能为空");
		}
		if (pictureEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		PictureEntity oldPictureEntity=pictureService.getById(pictureEntity.getId());
		if (null==oldPictureEntity) {
			json.getErrors().put("name", "不存在");
			return json;
		}
		oldPictureEntity.setClassId(pictureEntity.getClassId());
		oldPictureEntity.setContent(content);
		oldPictureEntity.setLastUpdateDate(new Date());
		oldPictureEntity.setName(pictureEntity.getName());
		pictureService.update(oldPictureEntity);
		json.setResult(true);
		return json;
	}
}
